<!-- 系统下拉框 -->
<template>
  <px-form-item
    label="系统"
    :prop="props.propValue ? props.propValue : 'systemCode'"
    :rules="{ required: props.isRequired, message: '系统不能为空', trigger: ['blur', 'change'] }"
  >
    <px-tree-select
      :model-value="systemValue"
      :disabled="props.isDisabled"
      clearable
      filterable
      :props="defaultProps"
      :data="systemList"
      check-strictly
      placeholder="请选择系统（树状图）"
      node-key="code"
      :default-expanded-keys="['FAC_83abf9', 'FAC_93abf6']"
      style="width: 100%"
      @node-click="nodeClick"
      @visible-change="visibleChangeSystem"
      @clear="clearSystem"
    />
  </px-form-item>
</template>

<script setup lang="ts">
import { querySystemTree } from "@/api/common";
import { querySystemSelect } from "@/api/equip/archive";
import { ref, watch } from "vue";

// 父级方法和值
const emits = defineEmits(["changeAllSystemCodes", "changeSystemCodes", "clearSystem", "getEditSystemCodes"]);
const props = defineProps(["className", "systemCode", "isRequired", "propValue", "isDisabled", "currentType"]);

const defaultProps = {
  children: "children",
  label: "name",
  value: "code",
  disabled: "disabled"
};
const systemValue = ref("");
const systemList = ref<any>([]);

const systemCodeArr = ref([]);

const getSystemItem = (list, id) => {
  for (let i in list) {
    if (list[i].code === id) {
      systemCodeArr.value.push(list[i].code);
      if (list[i].children && list[i].children.length > 0) {
        list[i].children.forEach(item => {
          systemCodeArr.value.push(item.code);
        });
      }
      return list[i];
    }
    if (list[i].children) {
      let node = getSystemItem(list[i].children, id);
      if (node !== undefined) {
        return node;
      }
    }
  }
};

watch(
  () => props.systemCode,
  val => {
    systemValue.value = props.systemCode;
    systemCodeArr.value = [];
    if (val) {
      setTimeout(async () => {
        if (props.className) {
          const tempSystemList = await querySystemTree({ className: props.className });
          systemList.value = tempSystemList || [];
          if (props.currentType === "edit" || props.currentType === "EDIT" || props.currentType === "copy" || props.currentType === "COPY") {
            getSystemItem(tempSystemList, val);
            emits("getEditSystemCodes", val, systemCodeArr.value);
          }
        } else {
          const tempSystemList = await querySystemSelect({ className: props.className });
          tempSystemList.forEach(item => {
            if (item.code === "FAC_83abf9" || item.code === "FAC_93abf6") {
              item.disabled = true;
            }
          });
          systemList.value = tempSystemList || [];
        }
      }, 0);
    }
  },
  { immediate: true }
);

// 显示隐藏科室下拉列表
const visibleChangeSystem = async val => {
  if (val) {
    if (props.className) {
      const tempSystemList = await querySystemTree({ className: props.className });
      systemList.value = tempSystemList || [];
    } else {
      const tempSystemList = await querySystemSelect({ className: props.className });
      tempSystemList.forEach(item => {
        if (item.code === "FAC_83abf9" || item.code === "FAC_93abf6") {
          item.disabled = true;
        }
      });
      systemList.value = tempSystemList || [];
    }
  } else {
    systemList.value = [];
  }
};
// 点击左侧树节点，获取工厂、科室、系统、系统codes
const nodeClick = (e1emData, nodeData) => {
  if (props.className) {
    const codes = [e1emData.code];
    if (e1emData.children && e1emData.children.length > 0) {
      e1emData.children.forEach(item => {
        codes.push(item.code);
      });
    }
    emits("changeSystemCodes", e1emData.code, codes);
  } else {
    if (e1emData.type === "factoryArea") {
      // 一级工厂
      // emits("changeAllSystemCodes", e1emData.code, "", e1emData.code, []);
      // emits("changeAllSystemCodes", e1emData.code, "", "", []);
    } else if (e1emData.type === "className") {
      // 二级科室
      //emits("changeAllSystemCodes", nodeData.parent.data.code, e1emData.code, e1emData.code, []);
      emits("changeAllSystemCodes", nodeData.parent.data.code, e1emData.code, "", []);
    } else if (e1emData.type === "systemCategory") {
      const codes = [e1emData.code];
      if (e1emData.children && e1emData.children.length > 0) {
        e1emData.children.forEach(item => {
          codes.push(item.code);
        });
      }
      if (nodeData.parent.data.type === "className") {
        // 三级系统
        emits("changeAllSystemCodes", nodeData.parent.parent.data.code, nodeData.parent.data.code, e1emData.code, codes);
      } else {
        if (nodeData.parent?.parent.data.type === "className") {
          // 四级系统
          emits("changeAllSystemCodes", nodeData.parent.parent.parent.data.code, nodeData.parent.parent.data.code, e1emData.code, codes);
        } else {
          // 五级系统
          emits(
            "changeAllSystemCodes",
            nodeData.parent.parent.parent.parent.data.code,
            nodeData.parent.parent.parent.data.code,
            e1emData.code,
            codes
          );
        }
      }
    }
  }
};
// 系统值改变触发
const clearSystem = () => {
  emits("clearSystem", systemValue.value);
};
</script>
